<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>导航栏吸顶 - 
        Navigation bar ceiling</title>
    <style>
        *{
              margin: 0;
              padding: 0;
          }
        body{
            height: 2000px;
            background-image: linear-gradient(-180deg, #15f09d 0%, #25A0FF 50%, #fca72b 100%);
        }
        #wrap{
            background-color: rgba(0,0,0,0);
            width: 100%;
            height: 100px;
            margin-top: 100px;
        }
        #wrap[data-fixed="fixed"]{
            position: fixed;
            top:0;
            left: 0;
            margin: 0;
        }
        </style>
</head>
<body>
    <div id="wrap"></div>
    </body>
    <script>
        var obj = document.getElementById("wrap");
        var ot = obj.offsetTop;
        document.onscroll = function () {
            if((document.documentElement.scrollTop) > 101){
                var wrap = document.getElementById("wrap");
                obj.setAttribute("data-fixed","fixed");
                wrap.style.background = 'rgba(0,0,0,0.2)';
            }else{
                var wrap = document.getElementById("wrap");
                obj.setAttribute("data-fixed","");
                wrap.style.background = 'rgba(0,0,0,0)';
            }
        console.log(ot);
        console.log();
    };
    </script>
    
</html>